package cn.e4j.modular.system.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.system.entity.SysMaillog;
import cn.e4j.modular.system.service.SysMaillogService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/maillog")
public class SysMailLogController extends BaseController {
    @Autowired
    SysMaillogService sysMaillogService;

    /**
     * 跳转用户管理页面
     */
    @RequiresPermissions("sys:maillog:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/maillog.html";
    }

    /**
     * 查询邮件参数列表
     */
    @RequestMapping("/getMaillogList")
    @RequiresPermissions("sys:maillog:init")
    @ResponseBody
    public ResponseData getMailList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysMaillogService.getMaillogList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:maillog:delete")
    @ResponseBody
    public ResponseData del(SysMaillog maillog) {

        String id = maillog.getID();
        // 注意：user 前端只能传需要修改值和主键值，以防把不需要修改的值改成空值
        maillog.deleteById();

        return SUCCESS_TIP;
    }

    @RequiresPermissions("sys:maillog:delete")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delRowsBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysMaillog> mailList = inDto.getReqList(SysMaillog.class);
        List<String> idList = new ArrayList<>();
        for (SysMaillog maillog : mailList) {
            idList.add(maillog.getID());
        }
        sysMaillogService.removeByIds(idList);
        return SUCCESS_TIP;
    }
}
